package com.jt.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.security.Provider;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/doRestEcho1")
    public String doRestEcho1(){
        String url = "http://localhost:8081/provider/echo/pro";
        return restTemplate.getForObject(url,String.class);
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Value("${spring.application.name:8090}")
    private String appName;

    @GetMapping("/consumer/doRestEcho2")
    public String doRestEcho02(){
        ServiceInstance instance = loadBalancerClient.choose("sca-provider");
        String ip = instance.getHost();
        int port = instance.getPort();
        String url = String.format("http://%s:%s/provider/echo/%s",ip,port,appName);
        return restTemplate.getForObject(url,String.class);
    }

    @Autowired
    private RestTemplate loadBalancedRestTemplate;

    @GetMapping("/consumer/doRestEcho3")
    public String doRestEcho03(){
        String url = String.format("http://sca-provider/provider/echo/%s",appName);
        String url2 = String.format("http://%s/provider/echo/%s","sca-provider",appName);
        return loadBalancedRestTemplate.getForObject(url,String.class);
    }
}
